<template>
  <div style="">
    <el-row :gutter="20">
      <el-col :span="8"><div class="grid-content ep-bg-purple" /></el-col>
      <el-col :span="8"><div class="grid-content ep-bg-purple" /></el-col>
      <el-col :span="4" style="display: inline-block"><div class="grid-content ep-bg-purple" />
        <div class="welcome-message">
          <span>用户, 欢迎您！</span>
        </div>
      </el-col>
      <el-col :span="4"><div class="grid-content ep-bg-purple" />
        <div class="dropdown">
          <span style="font-size: 18px;">{{ user_name }}</span>
          <el-dropdown>
            <el-icon style="margin-left: 15px; margin-top: 5px; color: #ffffff; font-size: 24px;position: fixed"><ArrowDown />
            </el-icon>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="$router.push('/demo04')" class="logout-item">个人中心</el-dropdown-item>
                <!--              <el-dropdown-item @click="$router.push('/')">退出登录</el-dropdown-item>-->
                <el-dropdown-item @click="logout" class="logout-item">安全退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import VueCookies from 'vue-cookies'
import { ElMessageBox, ElMessage } from 'element-plus'
// import type { Action } from 'element-plus'
export default {
  data () {
    return {
      user_name: ''
    }
  },
  created () {
    // 构造函数自动触发
    // this.logout()
  },
  mounted () {
    const userData = VueCookies.get('rememberMe')
    // console.log(userData)
    // 取name
    this.user_name = userData.name
    this.$root.toggleNav()
  },
  beforeUnmount () {
    this.$root.toggleNav()
  },
  methods: {
    logout () {
      ElMessageBox.confirm(
        '此操作将退出登录. 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          // draggable: true 可移动
        })
        .then(() => {
          ElMessage({
            type: 'success',
            message: '安全退出成功'
          })
          VueCookies.remove('rememberMe') // 清除 cookie
          this.$router.replace('/') // 替换路由，不留下历史记录
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消成功'
          })
        })
    }
  },
  components: {
  }
}
</script>
<style>
.welcome-message {
  padding: 20px 0 10px; /* 调整填充以提高响应性 */
  float: right;
  white-space: nowrap; /* 防止文本换行 */
  overflow: hidden; /* 隐藏溢出的内容 */
  text-overflow: ellipsis; /* 用省略号（...）表示溢出的内容 */
}

.dropdown {
  vertical-align: middle;
  line-height: 40px;
  padding-top: 10px;
  margin-right: 8px; /* 调整边距以提高间距 */
  white-space: nowrap; /* 防止文本换行 */
}
.dropdown el-avatar{
  width: 50px;
  height: 50px;
}

/* 下拉图标的新样式 */
.dropdown-icon {
  margin-left: 5px;
  margin-top: 5px;
  color: #ffffff;
  font-size: 24px;
  position: fixed;
}
.logout-item:hover {
  background-color: #1890ff;
}
</style>
